﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace XLAction
{
    public class ApiAuthorize : IAuthorizationFilter
    {
        public async void OnAuthorization(AuthorizationFilterContext context)
        {
            if (context.Filters.Contains(new MyNoAuthentication()))
            {
                return;
            }

            #region 用户请求限流
            {
                string ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
                var cotrollaction = context.ActionDescriptor;
                string action = cotrollaction.RouteValues["action"].ToString();
                string controller = cotrollaction.RouteValues["controller"].ToString();
                if (string.IsNullOrWhiteSpace(ip) || string.IsNullOrWhiteSpace(controller) || string.IsNullOrWhiteSpace(action))
                {
                    context.Result = new JsonResult("系统正忙，请稍微再试！") { StatusCode = StatusCodes.Status429TooManyRequests };
                    return;
                }
                ip = ip + ":" + controller + ":" + action;
                IPCacheInfoModel ipModel = IPCacheHelper.GetIPLimitInfo(ip);
                if (!ipModel.IsVisit)
                {
                    context.Result = new JsonResult("系统正忙，请稍微再试！") { StatusCode = StatusCodes.Status429TooManyRequests };
                    return;
                }
                string ACting = controller + ":" + action;
                IPCacheInfoModel ipModel2 = IPCacheHelper.GetIPLimitInfo(ACting);


            }
            #endregion




        }

    }
}
